package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
)

const (
	version = "v1.0.0"
)

func Hello(w http.ResponseWriter, req *http.Request) {
	fmt.Println(*req)
	// call backendservice
	fmt.Println("calling backendservice...")
	resp, err := http.Get("http://httpbackend.service.dc1.consul:8081")
	if err != nil {
		io.WriteString(w, fmt.Sprintf("this is httpfrontservice, version: %s, calling backendservice error: %s\n", version, err))
		return
	}
	fmt.Println(*resp)
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		io.WriteString(w, fmt.Sprintf("this is httpfrontservice, version: %s, calling backendservice error: %s\n", version, err))
		return
	}

	fmt.Println(string(body))

	io.WriteString(w, fmt.Sprintf("this is httpfrontservice, version: %s, calling backendservice ok, its resp: [%s]\n", version, string(body)))
}

func main() {
	http.HandleFunc("/", Hello)
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
